//
//  JGCategoryViewController.m
//  JGMeiTuanHD
//
//  Created by JJetGu on 15/9/10.
//  Copyright (c) 2015年 Free. All rights reserved.
//

#import "JGCategoryViewController.h"
#import "JGHomeDropdown.h"
#import "UIView+Extension.h"
#import "JGCategory.h"
#import "JGMetaTool.h"
#import "JGConst.h"

@interface JGCategoryViewController () <JGHomeDropdownDataSource, JGHomeDropdownDelegate>

@end

@implementation JGCategoryViewController

-(void)loadView
{
    JGHomeDropdown *dropdown = [JGHomeDropdown dropdown];
    dropdown.dataSource = self;
    dropdown.delegate = self;
    self.view = dropdown;
    
    // 设置控制器view在popover中的尺寸
    self.preferredContentSize = dropdown.size;
}

- (void)viewDidLoad {
    [super viewDidLoad];

}

#pragma mark - JGHomeDropdownDataSource
-(NSInteger)numberOfRowsInMainTable:(JGHomeDropdown *)homeDropdown
{
    return [JGMetaTool categories].count;
}

-(NSString *)homeDropdown:(JGHomeDropdown *)homeDropdown titleForRowInMainTable:(NSInteger)row
{
    JGCategory *category = [JGMetaTool categories][row];
    return category.name;
}

-(NSString *)homeDropdown:(JGHomeDropdown *)homeDropdown iconForRowInMainTable:(NSInteger)row
{
    JGCategory *category = [JGMetaTool categories][row];
    return category.small_icon;
}

-(NSString *)homeDropdown:(JGHomeDropdown *)homeDropdown selectedIconForRowInMainTable:(int)row
{
    JGCategory *category = [JGMetaTool categories][row];
    return category.small_highlighted_icon;
}
-(NSArray *)homeDropdown:(JGHomeDropdown *)homeDropdown subdataForRowInMainTable:(NSInteger)row
{
    JGCategory *category = [JGMetaTool categories][row];
    return category.subcategories;
}

#pragma mark - JGHomeDropdownDelegate
-(void)homeDropdown:(JGHomeDropdown *)homeDropdown didSelectRowInMainTable:(NSInteger)row
{
    JGCategory *category = [JGMetaTool categories][row];
    if (category.subcategories.count == 0) {
        // 发出通知
        [JGNotificationCenter postNotificationName:JGCategoryDidChangeNotification object:nil userInfo:@{JGSelectCategory : category}];
    }
}

-(void)homeDropdown:(JGHomeDropdown *)homeDropdown didSelectRowInSubTable:(NSInteger)subrow inMainTable:(int)mainRow
{
     JGCategory *category = [JGMetaTool categories][mainRow];
    
    // 发出通知
    [JGNotificationCenter postNotificationName:JGCategoryDidChangeNotification object:nil userInfo:@{JGSelectCategory : category, JGSelectSubcategoryName: category.subcategories[subrow]}];
}

@end
